/*
 * libkysdk-system's Library
 *
 * Copyright (C) 2023, KylinSoft Co., Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: Yunhe Liu <liuyunhe@kylinos.cn>
 *
 */

package kylin.kysdk.java.method;

import kylin.kysdk.java.client.DBusClient;
import com.kylin.kysdk.process;
import lombok.extern.slf4j.Slf4j;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
import java.util.List;

@Slf4j
public class ProcessMethod implements process {

    @Override
    public double getProcInfoCpuUsage(int proc){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            process obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/process", process.class);
            return obj.getProcInfoCpuUsage(proc);
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException GetEUser :",e);
        }
        return -1.0;
    }

    @Override
    public double getProcInfoIoUsage(int proc){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            process obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/process", process.class);
            return obj.getProcInfoIoUsage(proc);
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException GetEUser :",e);
        }
        return -1.0;
    }

    @Override
    public double getProcInfoMemUsage(int proc){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            process obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/process", process.class);
            return obj.getProcInfoMemUsage(proc);
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException GetEUser :",e);
        }
        return -1.0;
    }
    @Override
    public String getProcInfoStatus(int proc){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            process obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/process", process.class);
            return obj.getProcInfoStatus(proc);
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException GetEUser :",e);
        }
        return null;
    }

    @Override
    public int getProcInfoPort(int proc){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            process obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/process", process.class);
            return obj.getProcInfoPort(proc);
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException GetEUser :",e);
        }
        return -1;
    }

    @Override
    public String getProcInfoStartTime(int proc){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            process obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/process", process.class);
            return obj.getProcInfoStartTime(proc);
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException GetEUser :",e);
        }
        return null;
    }

    @Override
    public String getProcInfoRunningTime(int proc){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            process obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/process", process.class);
            return obj.getProcInfoRunningTime(proc);
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException GetEUser :",e);
        }
        return null;
    }

    @Override
    public String getProcInfoCpuTime(int proc){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            process obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/process", process.class);
            return obj.getProcInfoCpuTime(proc);
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException GetEUser :",e);
        }
        return null;
    }

    @Override
    public String getProcInfoCmd(int proc){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            process obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/process", process.class);
            return obj.getProcInfoCpuTime(proc);
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException GetEUser :",e);
        }
        return null;
    }

    @Override
    public String getProcInfoUser(int proc){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            process obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/process", process.class);
            return obj.getProcInfoUser(proc);
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException GetEUser :",e);
        }
        return null;
    }

    @Override
    public List<String> getProcInfo(String proc){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            process obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/process", process.class);
            return obj.getProcInfo(proc);
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException GetEUser :",e);
        }
        return null;
    }
    @Override
    public List<String> getAllProcInfo(){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            process obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/process", process.class);
            return obj.getAllProcInfo();
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException GetEUser :",e);
        }
        return null;
    }

    @Override
    public boolean isRemote() {
        return false;
    }

    @Override
    public String getObjectPath() {
        return null;
    }
}
